﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using QLHXNK.BusinessLogic;

namespace QLHXNK
{
    public partial class FrmBaoCaoNhapHang : Form
    {
        #region Properties
        int ItemID = -1;
	    #endregion


        #region Form funtions
        public FrmBaoCaoNhapHang()
        {
            InitializeComponent();

            //Load DS ma hang:
            getItemList();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dtpickerDenNgay_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {
            doPrintReport();
        }

        private void cmbMaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
      
            try
            {
                ItemID = int.Parse(cmbMaHang.SelectedValue.ToString());
            }
            catch
            {
                ItemID = -1;
            }

            if (ItemID !=-1)
            {
                DataTable table = Item.getItemList(String.Format("SELECT * FROM Item WHERE ID = {0}", ItemID));
                if (table != null && table.Rows.Count > 0)
                {
                    txtTenHang.Text = table.Rows[0]["item_name"].ToString();       
                }
                else
                {
                    txtTenHang.Text = "";
                }
            }
            else
            {
                txtTenHang.Text = "";
                if (cmbMaHang.Items.Count>0)
                    cmbMaHang.SelectedIndex=0;
            }
        }
        #endregion

        #region Hàm hỗ trợ
        private void doPrintReport()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            DateTime fromDate = dtpickerTuNgay.Value;
            DateTime toDate = dtpickerDenNgay.Value;

            dlg.FileName = "Bao cao nhap hang tu" + fromDate.Day.ToString() + "-" + fromDate.Month.ToString() + "-" + fromDate.Year.ToString() + " den " + toDate.Day.ToString() + "-" + toDate.Month.ToString() + "-" + toDate.Year.ToString() + ".xls";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                String filePath = dlg.FileName;
                MessageBox.Show(filePath + "\nChưa xong phần này");
                //DataTable table = new DataTable();
            }
        }

        private void getItemList()
        {
            DataTable table = Item.getItemList("SELECT ID, item_name, item_code FROM Item");

            if (table == null)
            {
                table = new DataTable();
                table.Columns.Add("item_code");
                table.Columns.Add("item_name");
                table.Columns.Add("ID");
            }

            DataRow row = table.NewRow();
            row["ID"] = -1;
            row["item_code"] = "Chọn hàng hóa";
            row["item_name"] = "";
            table.Rows.InsertAt(row, 0);

            cmbMaHang.ValueMember = "ID";
            cmbMaHang.DisplayMember = "item_code";
            cmbMaHang.DataSource = table;
        }
        #endregion

        
        
    }
}